﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using System.Speech.Synthesis;
using System.Media;

namespace CommHelper
{
    /// <summary>
    /// 语音朗读类
    /// </summary>
    public sealed class SpeakHelper
    {

        #region 声明
        ///<summary> 
        ///朗读人 
        ///</summary> 
        private static SpeechSynthesizer _speaker;
        ///<summary>
        ///语音合成 
        ///</summary> 
        private static SpeechSynthesizer _voice;
        ///<summary> 
        ///朗读人线程 
        ///</summary> 
        private static Thread _speakerThread;
        ///<summary> 
        ///朗读语句 
        ///</summary> 
        private static ArrayList _wordsList = new ArrayList();
        ///<summary> 
        ///播放标志 
        ///</summary> 
        private static bool _playFlags;
        #endregion
        #region 方法
        ///<summary> 
        ///构造函数 
        ///</summary> 
        private SpeakHelper()
        {
            this.StartUp();
        }

        private static SpeakHelper _SpeakHelper;
        /// <summary>
        /// 朗读入口
        /// </summary>
        public static SpeakHelper Instance
        {
            get
            {
                if (_SpeakHelper == null)
                {
                    _SpeakHelper = new SpeakHelper();
                }
                return _SpeakHelper;
            }
        }
        ///<summary> 
        ///朗读人 
        ///</summary> 
        private void SpeakerProperty(string words)
        {
            PromptBuilder builder = new PromptBuilder();
            _speaker.Rate = 1; _speaker.Volume = 100;
            _speaker.SetOutputToDefaultAudioDevice();
            builder.AppendText(words);

            _speaker.Speak(builder);
        }
        ///<summary> 
        ///语音朗读 
        ///</summary> 
        private void SoundPlay()
        {
            ArrayList datacopy = new ArrayList();
            _playFlags = true;
            int index = 0;
            _speaker = new SpeechSynthesizer();//朗读人初始化  
            while (_playFlags)
            {
                Thread.Sleep(100);
                lock (_wordsList.SyncRoot)
                {
                    datacopy = _wordsList;
                }
                if (datacopy == null || datacopy.Count == 0 || index >= datacopy.Count)
                {
                    continue;
                }
                string words = (string)datacopy[index];
                this.SpeakerProperty(words);
                index++;
            }
        }
        ///<summary>
        /// 增加朗读语句 
        ///</summary> 
        public void AddSound(string words)
        {
            ArrayList datacopy = new ArrayList();
            lock (_wordsList.SyncRoot)
            {
                datacopy = _wordsList;
            }
            //if (datacopy != null && datacopy.Count != 0)
            //{
            //    if (((string)datacopy[datacopy.Count - 1]).Equals(words))
            //    {
            //        return;
            //    }
            //}
            datacopy.Add(words);
        }
        ///<summary> 
        ///朗读暂停 
        ///</summary> 
        public void PauseSound()
        {
            _speaker.Pause();
        }
        ///<summary> 
        ///朗读继续 
        ///</summary> 
        public void RusumeSound()
        {
            _speaker.Resume();
        }
        ///<summary> 
        ///语音保存 
        ///</summary> 
        public bool SaveFile(string words, string path)
        {
            _voice = new SpeechSynthesizer();
            PromptBuilder builder = new PromptBuilder();
            _voice.Rate = 1;
            _voice.Volume = 100;
            _voice.SetOutputToDefaultAudioDevice();
            builder.AppendText(words);

            _voice.SetOutputToWaveFile(path);
            _voice.Speak(builder);
            _voice.Dispose();
            return true;
        }

        ///<summary> 
        ///朗读服务启动 
        ///</summary> 
        public void StartUp()
        {
            _speakerThread = new Thread(new ThreadStart(this.SoundPlay));
            _speakerThread.Start();
        }
        ///<summary> 
        ///朗读服务关闭 
        ///</summary> 
        public void CloseDown()
        {
            _playFlags = false;
            if (_speaker != null)
            {
                _speaker.Dispose();
            }
            if (_speakerThread != null)
            {
                _speakerThread.Abort();
            }
            lock (_wordsList.SyncRoot)
            {
                _wordsList.Clear();
            }
        }
        #endregion
    }
}
